package com.superjson.showpro.shared.util;

import com.superjson.showpro.shared.exception.GeneralBizException;
import org.slf4j.Logger;

import static com.beantools.util.ObjectUtil.isNotNull;
import static java.math.BigDecimal.*;

/**
 * Created By zby on 16:52 2019/6/21
 */

public class LogUtil {

    public static void info(Logger logger, Exception e) {
        if (!log(logger, e, ROUND_UP) && isNotNull(e)) {
            logger.info(e.getMessage(), e.getCause());
        }

    }

    public static void info(Logger logger, String str) {
        logger.info(str);
    }

    public static void info(String msg, Logger logger, Exception e) {
        if (!log(logger, e, ROUND_UP) && isNotNull(e)) {
            logger.info(msg, e.getMessage(), e.getCause());
        }
    }

    public static void warn(Logger logger, Exception e) {
        if (!log(logger, e, ROUND_DOWN) && isNotNull(e)) {
            logger.warn(e.getMessage(), e.getCause());
        }
    }

    public static void warn(String msg, Logger logger, Exception e) {
        if (!log(logger, e, ROUND_DOWN) && isNotNull(e)) {
            logger.warn(msg, e.getMessage(), e.getCause());
        }
    }

    public static void debug(Logger logger, Exception e) {
        if (!log(logger, e, ROUND_CEILING) && isNotNull(e)) {
            logger.debug(e.getMessage(), e.getCause());
        }
    }

    public static void debug(String msg, Logger logger, Exception e) {
        if (!log(logger, e, ROUND_CEILING) && isNotNull(e)) {
            logger.debug(msg, e.getMessage(), e.getCause());
        }
    }

    public static void error(Logger logger, Exception e) {
        if (!log(logger, e, ROUND_FLOOR) && isNotNull(e)) {
            logger.error(e.getMessage(), e.getCause());
        }
    }

    public static void error(String msg, Logger logger, Exception e) {
        if (!log(logger, e, ROUND_FLOOR) && isNotNull(e)) {
            logger.error(msg, e.getMessage(), e.getCause());
        }
    }

    private static boolean log(Logger logger, Exception e, int index) {
        if (isNotNull(logger) && e instanceof GeneralBizException) {
            GeneralBizException exception = (GeneralBizException) e;
            switch (index) {
                case 0:
                    logger.info(exception.getErrMsg(), ((GeneralBizException) e).getResultCodeEnum());
                    return true;
                case 1:
                    logger.warn(exception.getErrMsg(), ((GeneralBizException) e).getResultCodeEnum());
                    return true;
                case 2:
                    logger.debug(exception.getErrMsg(), ((GeneralBizException) e).getResultCodeEnum());
                    return true;
                default:
                    logger.error(exception.getErrMsg(), ((GeneralBizException) e).getResultCodeEnum());
                    return true;
            }
        }
        return false;
    }

}
